#!/usr/bin/env python
#-*- coding:utf-8 -*-

import sqlite3
import csv
import re
import teste
import wx
import time


def csvToSql(Gauge):
    
    conexao = sqlite3.connect("dbByPython.db")
    c = conexao.cursor()
    
    c.execute('''drop table if exists rebides''')
    
    conexao.commit()
    
    c.execute('''create table if not exists rebides (Codigo_do_Docente integer, Nome text, ultimo_Grau text, Curso text, Categoria text, Regime_de_Tempo text, Ano_Lectivo text, Tipo_de_Estabelecimento text, Nome_do_Estabelecimento text)''')
    
    i = 0
    
    while(i < 11):
        csvName = 'reb%d.csv' % i
        spamReader = csv.reader(open(csvName, 'rb'), delimiter=',')
        for row in spamReader:
            if(row[1] == 'Nome'):
                continue
                pass
            try:       
                stmt = '''insert into rebides values ("{0}", "{1}", "{2}", "{3}", "{4}", "{5}", "{6}", "{7}", "{8}")'''.format(str(row[0]).replace("\"", ""),\
                str(row[1]).replace("\"", ""),\
                str(row[2]).replace("\"", ""),\
                str(row[3]).replace("\"", ""),\
                str(row[4]).replace("\"", ""),\
                str(row[5]).replace("\"", ""),\
                str(row[6]).replace("\"", ""),\
                str(row[7]).replace("\"", ""),\
                str(row[8]).replace("\"", ""))
            
                c.execute(stmt)
            except:
                break
                pass
            pass
        i += 1
        Gauge.SetValue(i)
        pass
    
    conexao.commit()
    conexao.close()
    
